﻿using UnityEngine.UIElements;

namespace winS.UnityEditor.UIElement
{
    public class IntField : EditorField<int>
    {
        private readonly IntegerField integerField;

        public IntField(ElementFactory elementFactory) : base(elementFactory)
        {
            Add(integerField = new IntegerField());
            EditorElementUtility.ApplySubFieldStyle(integerField);
            integerField.RegisterValueChangedCallback(OnValueChangedCallback);
        }

        public override void SetValueWithoutNotify(int value)
        {
            base.SetValueWithoutNotify(value);
            integerField.SetValueWithoutNotify(value);
        }

        protected override void SetValue(int value)
        {
            base.SetValue(value);
            integerField.SetValueWithoutNotify(value);
        }
        protected override void OnRedo(int newValue)
        {
            base.OnRedo(newValue);
            integerField.SetValueWithoutNotify(value);
        }
        protected override void OnUndo(int oldValue)
        {
            base.OnUndo(oldValue);
            integerField.SetValueWithoutNotify(value);
        }

        private void OnValueChangedCallback(ChangeEvent<int> changeEvent)
        {
            base.SetValue(changeEvent.newValue);
        }
    }
}
